/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testadventure;

import edu.clemson.cs.hamptos.adventure.AdventureCommand;
import edu.clemson.cs.hamptos.adventure.AdventureEngine;
import edu.clemson.cs.hamptos.adventure.AdventureTarget;
import edu.clemson.cs.hamptos.adventure.AdventureWindow;
import edu.clemson.cs.hamptos.adventure.DoNotUnderstandException;
import java.util.Iterator;

/**
 *<p>An enemy target, blocks all other input except its own when active</p>
 * @author Nicks
 */
public class ItemEnemy extends ItemFull {
    /**
     * <p>Constructor for a new ItemEnemy class that inherits ItemFull</p>
     * @param inBlob - of type StringBuilder 
     */
    public ItemEnemy(StringBuilder inBlob)
    {
        super(inBlob);
    }
    /**
     * <p>Constructor for a new ItemEnemy object</p>
     */
    protected ItemEnemy() {
    }
    
    @Override
    public void doCommandTo(AdventureCommand c, AdventureEngine e, 
                            AdventureWindow w) 
                            throws DoNotUnderstandException {
        //check to see if the verb is acceptable to the do
        if (stats.get("enabled") == 1)
        {
            w.println("You try to " + c.getVerb() + " " + c.getDirectObject().getShortDescription()
                        + " and you get eaten...");
            e.gameOver();
        }
        else
        {
            w.println("The zombie is already dead, well dead-er, better not " + c.getVerb());
        }
    }
    
    @Override
    public void doCommandWith(AdventureCommand c, AdventureEngine e, 
                            AdventureWindow w) 
                            throws DoNotUnderstandException {
        if (stats.get("enabled") == 1)
        {
            w.println("You try to " + c.getVerb() + " " + getShortDescription()
                        + " and you get eaten...");
            e.gameOver();
        }
        else
        {
            w.println("The zombie is already dead, well dead-er, better not " + c.getVerb());
        }
    }
    
    protected boolean dead = false;
    
    @Override
    public void listen(AdventureEngine e, AdventureWindow w) {
        int enable;
        if ((stats.get("enabled") == 0)&&(!dead))
        {
            e.startEvil(stats.get("deathtick"));
            stats.put("enabled", 1);
            enable = 0;
        }
        else
        {
            enable = 1;
            stats.put("enabled", 0);
            dead = true;
        }
        Iterator<AdventureTarget> itTargs = e.getPlayerLocation().getLocalTargets().iterator();
        while (itTargs.hasNext())
        {
            AdventureTarget targ = itTargs.next();
            if(!(targ.getShortDescription().equals(getShortDescription())))
            {
                targ.getStats().put("enabled", enable);
                addListener(targ.getName());
            }
        }
        if (dead)
        {
            e.stopEvil();
            listeners.clear();
        }
    }
    
    protected void fillClone(ItemEnemy cl)
    {
        super.fillClone(cl);
    }
    
    @Override
    public AdventureTarget clone()
    {
        ItemEnemy cl = new ItemEnemy();
        fillClone(cl);
        
        return cl;
    }
}
